package com.yy.crypto;

import cn.hutool.crypto.symmetric.SymmetricCrypto;

import java.nio.charset.StandardCharsets;

/**
 * 对称加密
 * 国密算法 SM4
 * @Author: lyy
 * Date: 2021/12/15 17:52
 */
public class SM4Util {

	private static final String KEY = "1234567812345678";
	private static final String ALGORITHM = "SM4/ECB/PKCS5Padding";

	public static void main(String[] args) {
		String content = "test中文";
		String result = encrypt(content);
		System.out.println(result);
		System.out.println(decrypt(result));
	}

	public static String encrypt(String content){
		SymmetricCrypto sm4 = new SymmetricCrypto(ALGORITHM, KEY.getBytes());
		return sm4.encryptHex(content);
	}

	public static String decrypt(String encryptHex){
		SymmetricCrypto sm4 = new SymmetricCrypto(ALGORITHM, KEY.getBytes());
		return sm4.decryptStr(encryptHex, StandardCharsets.UTF_8);
	}

}
